(function() {
    angular.module('app').component('toolSearchComponent', {
        templateUrl: 'app/components/tool.search.component/tool.search.component.html',
        controller: toolSearchComponent,
        controllerAs: 'vm',
        bindings: {
            generateConfig: '<', //obj->{}
            filter: '<'
        }
    });

    toolSearchComponent.$inject = ['$scope','util', '$timeout','$rootScope'];

    function toolSearchComponent($scope,util, $timeout,$rootScope) {
        var vm = this;

        vm.option = vm.generateConfig; //复制

        // bind
        //vm.filter 
        //q 关键词  title 标题 category 类目  shopType 店铺类型  price 成交均价  sellAmount 销售量 time 上新时间

        /*vm.generateConfig->{
            category: {},
            q:'',
            shopType: '',
            iFashtion: '',
            title: '',
            sellAmount: '',
            price: '',
            time: '',
            noBtn
        }*/
        vm.copy = {};

        vm.shopTypeChange = shopTypeChange;//function

        vm.search = search; //function

        vm.reset = reset; //function

        vm.Enter = Enter; //function

        vm.mainCatSelect = mainCatSelect;//function

        var watchFun = $scope.$watch('vm.copy.category', function() {


            //console.log('-------------------------------vm.copy.category');
            //console.log(vm.copy.category);
            
            vm.search();
        });

        var watchFun2 =$scope.$watch('vm.copy.shopType', function() {
            //console.log('-------------------------------vm.copy.shopType');
            //console.log(vm.copy.shopType);
            
            vm.search();
        });
        

        function shopTypeChange(type){
            console.log(type);
        }

        //根据vm.generateConfig生成vm.copy
        vm.$onInit = function() {
            // console.log(vm.generateConfig);
            // console.log(vm.generateConfig.title)
            init();
        };



        function init() {
            for (var i in vm.generateConfig) {
                generate(i);
            }
        }

        //生成vm.copy
        function generate(generateType) {
            switch (generateType) {
                case 'mainCat':
                    vm.copy.mainCat = '';
                    vm.mainCategorys = [];
                    _getMainCategorys();
                    break;
                case 'q':
                    vm.copy.q = '';
                    break;
                case 'title':
                    vm.copy.title = '';
                    break;
                case 'category':
                    vm.copy.category = '';
                    break;
                case 'shopType':
                    vm.copy.shopType = '';
                    break;
                case 'iFashtion':
                    vm.copy.iFashtion = 0;
                    break;
                case 'price':
                    vm.copy.highPrice = ''; //售价-最低
                    vm.copy.lowPrice = ''; //售价-最高
                    break;
                case 'sellAmount':
                    vm.copy.lowSellAmount = '';
                    vm.copy.highSellAmount = '';
                    break;
                case 'time':
                    vm.copy.lowImportTime = '';
                    vm.copy.highImportTime = '';
                    break;
                default:
                    break;
            }
        }

        function _getMainCategorys() {

            //获取店铺的种类
            util.$get({
                url: '/marketconditions/category/getMainCategory.htm'
            }).then(function(resp) {

                vm.mainCategorys = resp.value.mainCategorys;

            }, function(err) {

            });
        }

        //主营行业
        function mainCatSelect(cat){
            
            vm.copy.mainCat = cat;

            vm.search();
        }


        function ifashionSelect() {
            if (typeof(vm.generateConfig.iFashtion) == "undefined") {
                // console.log('no iFashion');
                return;
            }

            if (vm.ifashionSelectStuff) {
                vm.copy.iFashtion = 1;
            } else {
                vm.copy.iFashtion = 0;
            }
        }


        function search() {
            ifashionSelect();
            if(angular.equals(vm.copy, vm.filter)){
                console.log("vm.copy 和 vm.,filter 相等");
                return;

            }

            console.log('===============================================');

            angular.extend(vm.filter,vm.copy);

            $rootScope.$emit('tool-search-component-emit-EV',vm.filter);

            
        }

        function reset() {

            for (var i in vm.copy) {
                if (i == 'category' || i == 'mainCat') {
                    //类目不能按钮重置
                    continue;
                }
                vm.copy[i] = '';
            }

            vm.search();
        }

        function Enter(e) {
            // console.log(e.keyCode);
            if (e.keyCode == 13) {
                if ( angular.element(document.querySelector('#btn_tool_ok')).hasClass('disabled')) {
                    // console.log('form error return');
                    return;
                }

                vm.search();
            } else {
                return;
            }   


        }
        vm.$onDestroy = function(){
            console.log('destory tool');
            watchFun();
            watchFun2();
        };
        
    }
})();
